package com.ciwong.epaper.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ciwong.epaper.R;
import com.ciwong.mobilelib.i.ViewOnClickListener;

/**
 * 播放状态控件 Created by lzr on 2017/4/13. 句子
 */
public class PlayStateView extends LinearLayout
{
    private ViewOnClickListener clickListener;

    private RoundProgressBar round_progress_bar;

    private ImageView playing;

    private ImageView recording;

    private ImageView img_stop_recording;

    private final int TIME_INTERVAL = 100;

    /**
     * 1 为播放状态 2为录音状态
     */
    private int isPlayButStatus = PLAY_STATUE;

    public static final int PLAY_STATUE = 1;

    public static final int RECORD_STATUE = 2;

    /**
     * ture 为播放状态 false为录音状态
     */
    private boolean isPlay = false;

    private Long timeCount;

    private int progress;

    private boolean isHandler = false;

    Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 0:
                    if (isPlay)
                    {
                        progress++;
                        round_progress_bar.setProgress(progress);
                        mHandler.sendEmptyMessageDelayed(0, TIME_INTERVAL);
                        isHandler = true;
                    }
                    else
                    {
                        mHandler.removeMessages(0);
                        isHandler = false;
                    }

                    break;
            }
        }
    };

    public PlayStateView(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);
        initView();
    }

    public PlayStateView(Context context, @Nullable AttributeSet attrs,
                         int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView()
    {
        setOrientation(LinearLayout.VERTICAL);
        setBackgroundColor(getResources().getColor(R.color.white));
        View.inflate(getContext(), R.layout.play_state_view, this);
        round_progress_bar = (RoundProgressBar) findViewById(
                R.id.round_progress_bar);
        playing = (ImageView) findViewById(R.id.problemPlay);
        recording = (ImageView) findViewById(R.id.img_micro_recording);
        img_stop_recording = (ImageView) findViewById(R.id.img_stop_recording);

    }

    public ViewOnClickListener getClickListener()
    {
        return clickListener;
    }

    public void setClickListener(ViewOnClickListener clickListener)
    {
        recording.setOnClickListener(clickListener);
        img_stop_recording.setOnClickListener(clickListener);
        this.clickListener = clickListener;
    }

    public void setTimeCount(Long timeCount)
    {
        this.timeCount = timeCount;
        int max = timeCount.intValue() / TIME_INTERVAL;
        round_progress_bar.setMax(max);

    }

    public void setTimeAndPlayBtnState(Long timeCount, int isPlayButStatus)
    {
        this.timeCount = timeCount;
        int max = timeCount.intValue() / TIME_INTERVAL;
        round_progress_bar.setMax(max);
        round_progress_bar.setProgress(0);
        this.isPlayButStatus = isPlayButStatus;
        if (isPlayButStatus == PLAY_STATUE)
        {
            playing.setVisibility(View.VISIBLE);
            recording.setVisibility(GONE);
        }
        else if (isPlayButStatus == RECORD_STATUE)
        {
            playing.setVisibility(View.GONE);
            recording.setVisibility(VISIBLE);
        }
        else
        {
            playing.setVisibility(View.GONE);
            recording.setVisibility(GONE);
        }
    }

    public void updatePlayProgress(long total, long progress)
    {
        // round_progress_bar.setVisibility(View.VISIBLE);
        // round_progress_bar.setMax(total);
        // round_progress_bar.setProgress(progress);
    }

    /**
     * 自动模式录音
     */
    public void startRecord()
    {
        setVisibility(VISIBLE);
        setPlayBtnState(false, View.GONE);
        setRecordState(true, View.GONE);
        isPlay = true;
        round_progress_bar.setProgress(0);
        round_progress_bar.setVisibility(VISIBLE);
        progress = 0;
        if (!isHandler)
        {
            mHandler.sendEmptyMessageDelayed(0, TIME_INTERVAL);
            isHandler = true;
        }
    }

    /**
     * 手动模式模式录音
     */
    public void startUnAutoRecord()
    {
        setVisibility(VISIBLE);
        isPlay = true;
        setUnAutoStopState(true);
        round_progress_bar.setProgress(0);
        round_progress_bar.setVisibility(VISIBLE);
        progress = 0;
        if (!isHandler)
        {
            mHandler.sendEmptyMessageDelayed(0, TIME_INTERVAL);
            isHandler = true;
        }
    }

    /**
     * 当独录音
     */
    public void startRecordHandler()
    {
        setVisibility(VISIBLE);
        setPlayBtnState(false, View.GONE);
        setRecordState(true, View.GONE);
        isPlay = true;
        round_progress_bar.setProgress(0);
        round_progress_bar.setVisibility(VISIBLE);
        progress = 0;
        mHandler.sendEmptyMessageDelayed(0, TIME_INTERVAL);
        isHandler = true;
    }

    /**
     * 播放
     */
    public void startPlay()
    {
        setVisibility(VISIBLE);
        setPlayBtnState(true, View.GONE);
        setRecordState(false, View.GONE);
        isPlay = true;
        round_progress_bar.setProgress(0);
        round_progress_bar.setVisibility(VISIBLE);
        progress = 0;
        mHandler.sendEmptyMessageDelayed(0, TIME_INTERVAL);
        isHandler = true;
    }

    /**
     * 设置播放图标的显示状态
     *
     * @param isShow
     */
    public void setPlayBtnState(boolean isShow, int showView)
    {
        if (playing == null)
        {
            return;
        }
        if (isShow)
        {
            playing.setVisibility(View.VISIBLE);
            Drawable drawable = playing.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable)
            {
                ((AnimationDrawable) drawable).start();
            }

        }
        else
        {
            playing.setVisibility(showView);
            Drawable drawable = playing.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable)
            {
                ((AnimationDrawable) drawable).stop();
            }
        }
    }

    // 设置录音图标的显示状态
    public void setRecordState(boolean isRecording, int showView)
    {
        if (null == recording)
        {
            return;
        }
        if (isRecording)
        {
            recording.setVisibility(VISIBLE);
            Drawable drawable = recording.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable)
            {
                ((AnimationDrawable) drawable).start();
            }
        }
        else
        {
            recording.setVisibility(showView);
            Drawable drawable = recording.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable)
            {
                ((AnimationDrawable) drawable).stop();
            }
        }
    }

    // 设置录音图标的显示状态
    public void setUnAutoRecordState(boolean showView)
    {
        setVisibility(VISIBLE);
        img_stop_recording.setVisibility(GONE);
        if (showView)
        {
            recording.setVisibility(VISIBLE);
        }
        else
        {
            recording.setVisibility(GONE);
        }
    }

    // 设置打卡录音图标的显示状态
    public void setDKAutoRecordState(boolean showView)
    {
        setVisibility(VISIBLE);
        img_stop_recording.setVisibility(GONE);
        if (showView)
        {
            recording.setVisibility(VISIBLE);
            round_progress_bar.setVisibility(GONE);
        }
        else
        {
            recording.setVisibility(GONE);
        }
    }

    // 设置暂停录音图标的显示状态
    public void setUnAutoStopState(boolean showView)
    {
        setVisibility(VISIBLE);
        recording.setVisibility(GONE);
        if (showView)
        {
            img_stop_recording.setVisibility(VISIBLE);
        }
        else
        {
            img_stop_recording.setVisibility(GONE);
        }
    }

    public boolean isPlay()
    {
        return isPlay;
    }

    public void closeView(boolean mIsUnAutoMode)
    {
        if (mIsUnAutoMode)
        {
            stopUnatoPlay();
        }
        else
        {
            stopPlay();
        }
    }
    //
    /**
     * 暂停手动模式
     */
    private void stopUnatoPlay()
    {
        setVisibility(INVISIBLE);
        setUnAutoRecordState(true);
        isPlay = false;
        round_progress_bar.setVisibility(View.GONE);
        mHandler.removeMessages(0);
        isHandler = false;
    }
    /**
     * 暂停并隐藏
     */
    private void stopPlay()
    {
        setPlayBtnState(false, View.GONE);
        setRecordState(false, View.GONE);
        setVisibility(INVISIBLE);
        isPlay = false;
        round_progress_bar.setVisibility(View.GONE);
        mHandler.removeMessages(0);
        isHandler = false;
    }

    /**
     * 暂停播放
     */
    public void stopOnlyPlay()
    {
        setRecordState(false, View.GONE);
        setPlayBtnState(false, View.VISIBLE);
        isPlay = false;
        round_progress_bar.setProgress(round_progress_bar.getMax());
        mHandler.removeMessages(0);
        isHandler = false;
    }

    /**
     * 暂停录音
     */
    public void stopOnlyRecord()
    {
        setPlayBtnState(false, View.GONE);
        setRecordState(false, View.VISIBLE);
        round_progress_bar.setProgress(round_progress_bar.getMax());
        mHandler.removeMessages(0);
        isHandler = false;
    }
}
